package cn.yuanqiao.web.controller.license.utils;

import cn.yuanqiao.common.license.AbstractServerInfos;
import cn.yuanqiao.common.license.impl.LinuxServerInfos;
import cn.yuanqiao.common.license.impl.WindowsServerInfos;
import cn.yuanqiao.common.license.utils.EncryptAESUtil;
import cn.yuanqiao.common.license.utils.EncryptRSAUtil;
import cn.yuanqiao.common.license.utils.LicenseCheckModel;
import com.alibaba.fastjson.JSONObject;

import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

public class CreateZhucema {

	/**
	 * 获取注册码
	 *
	 * return 注册码
	 **/
	public Map<String, String>  getZhucema(String key){
		//首先,判断windows还是linux
		//操作系统类型
		String osName = System.getProperty("os.name").toLowerCase();
		AbstractServerInfos abstractServerInfos = null;
		//根据不同操作系统类型选择不同的数据获取方法
		if (osName.startsWith("windows")) {
			abstractServerInfos = new WindowsServerInfos();
		} else if (osName.startsWith("linux")) {
			abstractServerInfos = new LinuxServerInfos();
		}else{//其他服务器类型
			abstractServerInfos = new LinuxServerInfos();
		}
		//获取
		LicenseCheckModel infos = abstractServerInfos.getServerInfos();

		//加密生成注册码
		Map<String, String> jiami = jiami(key,infos);
		return jiami;
	}

	/**
	 * 明文注册码加密
	 * infos 需要校验的License参数
	 * return  加密后的注册码
	 **/
	public Map<String,String> jiami(String key,LicenseCheckModel infos){

		Map<String,String> map=new HashMap<String, String>();
		// 授权码公钥：MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGJexeBreudyo0vj/ohjqzyoFm6GfqHM/+Xz+s/fja8D+WEwim9LsYQIz3yefvSGfIy0cgEdhH4Vh0SPVGoSPFVx9tv8iu6dEvHK4N3osQqdEPnd1p/QhMwjs4t3jqXmsDBrWZs8I8dU0eIBD1JKDSV336hp1vvruS1db2xYkx3wIDAQAB
		// 授权码公钥字符串
		String pukString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGJexeBreudyo0vj/ohjqzyoFm6GfqHM/+Xz+s/fja8D+WEwim9LsYQIz3yefvSGfIy0cgEdhH4Vh0SPVGoSPFVx9tv8iu6dEvHK4N3osQqdEPnd1p/QhMwjs4t3jqXmsDBrWZs8I8dU0eIBD1JKDSV336hp1vvruS1db2xYkx3wIDAQAB";
		try {
			RSAPublicKey puk = EncryptRSAUtil.getPublicKey(pukString);// 生成授权码公钥
			String jiamiKey = EncryptRSAUtil.encryptByPublicKey(key, puk);// 用授权码公钥加密 AES的key
			map.put("key", jiamiKey);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//AES对称加密-加密注册码信息    KEY ：   yqda_zcm_QWERGFD
		String mwzcm = JSONObject.toJSONString(infos);
		System.out.println("加密前：" + mwzcm);
		String encrypt = EncryptAESUtil.aesEncrypt(key,mwzcm);
		System.out.println("加密后：" + encrypt);
		String decrypt = EncryptAESUtil.aesDecrypt(key,encrypt);
		System.out.println("解密后：" + decrypt);
		map.put("zhucema", encrypt);
		return map;
	}

}
